package com.ccsoft.atools.service;

import com.ccsoft.atools.dao.SysUserRoleMapper;
import com.ccsoft.atools.model.SysUserRole;
import com.ccsoft.atools.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper dao;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public SysUserRole insert(SysUserRole record) {
        int ret = dao.insert(record);
        return record;
    }

    @Override
    public SysUserRole insertSelective(SysUserRole record) {
        int ret = dao.insertSelective(record);
        return record;
    }

    @Override
    public SysUserRole selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public SysUserRole updateByPrimaryKeySelective(SysUserRole record) {
        int ret = dao.updateByPrimaryKeySelective(record);
        return record;
    }

    @Override
    public SysUserRole updateByPrimaryKey(SysUserRole record) {
        int ret = dao.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public List<SysUserRole> selectAll() {
        return dao.selectAll();
    }

    @Override
    public List<SysUserRole> selectByRoleIid(int role_pk) {
        return dao.selectByRoleIid(role_pk);
    }

    @Override
    public int deleteByUserIid(int user_pk) {
        return dao.deleteByUserIid(user_pk);
    }

    @Override
    public int updateRoles(Integer userPk, List<Integer> rolePkList){
        if(userPk <= 0) return 0;
        dao.deleteByUserIid(userPk);

        if(rolePkList == null || rolePkList.size() == 0) return 0;
        rolePkList.forEach(rolePk -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUser_pk(userPk);
            sysUserRole.setRole_pk(rolePk);
            dao.insert(sysUserRole);
        });

        return rolePkList.size();
    }

}